package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 10071
 */
public class StoryUtil {
    static Pattern pattern = Pattern.compile("(\\【[^】]*\\】|\\([^)]*\\)|\\（[^）]*\\）)");

    public static List<String> splitText(String text) {
        List<String> result = new ArrayList<>();
        // 匹配【】或()及其内容的正则表达式
        Matcher matcher = pattern.matcher(text);

        int lastEnd = 0;
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();

            // 添加括号前的文本
            if (start > lastEnd) {
                result.add(text.substring(lastEnd, start));
            }

            // 添加括号及其内容
            result.add(text.substring(start, end));
            lastEnd = end;
        }

        // 添加最后一个括号后的文本
        if (lastEnd < text.length()) {
            result.add(text.substring(lastEnd));
        }
        return result;
    }
}

